import java.util.Observable;
import java.util.Observer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2021-06-19
 * Time: 10:14
 */
public class Observabletttt {
    public static void main(String[] args) {
//        ForecastProvider provider = new ForecastProvider("Sunny");
//        ForecastSubscriber subscriber = new ForecastSubscriber(provider);
//        provider.addObserver(subscriber);
//        provider.addObserver(new ForecastSubscriber(provider));
//        System.out.println(provider.countObservers());

        ForecastProvider provider = new ForecastProvider("Sunny");
        ForecastSubscriber subscriber = new ForecastSubscriber(provider);
        provider.addObserver(subscriber);
        provider.addObserver(new ForecastSubscriber(provider));
        provider.setMessage("Cloudy");
    }
}



class ForecastProvider extends Observable {
    private String weatherMessage = null;
    public ForecastProvider(String message){
        weatherMessage = message;
    }
    public void setMessage(String message){
        System.out.println("Updating the message and notifying the observers...");

        setChanged();
        notifyObservers(message);
    }
    public String getMessage() {
        return weatherMessage;
    }
}

class ForecastSubscriber implements Observer {
    private ForecastProvider provider = null;

    public ForecastSubscriber(ForecastProvider provider) {
        this.provider = provider;
    }

    public void update(Observable obs, Object obj) {
        System.out.println("The weather forecast has changed to: " + obj);
    }
}
